package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.service.wx.WxGoodsDetailService;
import com.cskaoyan.market.vo.wx.lhh.goodsVo.GoodsCommentVo;
import com.cskaoyan.market.vo.wx.lhh.goodsVo.SpecificationListVo;
import com.cskaoyan.market.vo.wx.lhh.homeIndexVo.NewHotGoodsListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WxGoodsDetailServiceImpl
 * @Description
 * @Author MZ
 * @Date 2023/5/21 15:22
 */
@Service
public class WxGoodsDetailServiceImpl implements WxGoodsDetailService {

    @Autowired
    MarketGoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    MarketGrouponRulesMapper grouponRulesMapper;
    @Autowired
    MarketIssueMapper issueMapper;
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;

    @Override
    public Map<String, Object> getGoodsDetail(Integer goodsId, Integer userId) {
        // 返回结果
        Map<String, Object> result = new HashMap<String, Object>();

        // specificationList 根据goodsId 查询specification表
        List<SpecificationListVo> specificationList = new ArrayList<>();
        // 首先查询规格
        MarketGoodsSpecificationExample sizeSpecificationExample = new MarketGoodsSpecificationExample();
        sizeSpecificationExample.or().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsId).andSpecificationEqualTo("规格");
        // 查询 其中根据
        List<MarketGoodsSpecification> sizeValueList = goodsSpecificationMapper.selectByExample(sizeSpecificationExample);
        SpecificationListVo sizeListVo = new SpecificationListVo();
        sizeListVo.setName("规格");
        sizeListVo.setValueList(sizeValueList);
        // 加入specificationList
        specificationList.add(sizeListVo);

        // 颜色
        MarketGoodsSpecificationExample colorSpecificationExample = new MarketGoodsSpecificationExample();
        colorSpecificationExample.or().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsId).andSpecificationEqualTo("颜色");
        List<MarketGoodsSpecification> colorValueList = goodsSpecificationMapper.selectByExample(colorSpecificationExample);
        if (colorValueList.size() != 0) {
            // 说明有颜色规格
            SpecificationListVo colorListVo = new SpecificationListVo();
            colorListVo.setName("颜色");
            colorListVo.setValueList(sizeValueList);
            // 加入specificationList
            specificationList.add(colorListVo);
        }
        // 加入result
        result.put("specificationList", specificationList);


        // groupon 查询groupon表，找到对应的groupon
        MarketGrouponRulesExample grouponRulesExample = new MarketGrouponRulesExample();
        grouponRulesExample.or().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsId);
        List<MarketGrouponRules> groupon = grouponRulesMapper.selectByExample(grouponRulesExample);
        // 加入result
        result.put("groupon", groupon);


        // issue
        MarketIssueExample issueExample = new MarketIssueExample();
        issueExample.or().andDeletedEqualTo(false);
        List<MarketIssue> issue = issueMapper.selectByExample(issueExample);
        // 加入result
        result.put("issue", issue);


        // userHasCollect 计数
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.or().andDeletedEqualTo(false).andValueIdEqualTo(goodsId);
        long userHasCollect = collectMapper.countByExample(collectExample);
        result.put("userHasCollect", userHasCollect);

        // shareImage
        // 加入result
        result.put("shareImage", "");

        // comment
        Map<String, Object> comment = new HashMap<>();
        List<GoodsCommentVo> data = new ArrayList<>();
        MarketCommentExample commentExample = new MarketCommentExample();
        commentExample.or().andDeletedEqualTo(false).andValueIdEqualTo(goodsId);
        List<MarketComment> commentList = commentMapper.selectByExample(commentExample);
        for (MarketComment marketComment : commentList) {

            GoodsCommentVo commentVo = new GoodsCommentVo();
            Integer voUserId = marketComment.getUserId();
            // 查询对应的user表，获得nickname和avatar
            MarketUser user = userMapper.selectByPrimaryKey(voUserId);
            commentVo.setAddTime(marketComment.getAddTime());
            commentVo.setPicList(marketComment.getPicUrls());
            commentVo.setAdminContent(marketComment.getAdminContent());
            commentVo.setNickname(user.getNickname());
            commentVo.setId(marketComment.getId());
            commentVo.setAvatar(user.getAvatar());
            commentVo.setContent(marketComment.getContent());
        }
        comment.put("data", data);
        // 计数
        MarketCommentExample countCommentExample = new MarketCommentExample();
        countCommentExample.or().andDeletedEqualTo(false).andValueIdEqualTo(goodsId);
        long count = commentMapper.countByExample(countCommentExample);
        comment.put("count", count);
        // 加入result
        result.put("comment", comment);


        // share
        // 加入result
        result.put("share", true);


        // attribute
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        attributeExample.or().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsId);
        List<MarketGoodsAttribute> attribute = attributeMapper.selectByExample(attributeExample);
        // 加入result
        result.put("attribute", attribute);

        // 先查询brandId
        MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
        Integer brandId = goods.getBrandId();
        MarketBrand brand = brandMapper.selectByPrimaryKey(brandId);
        // 加入result

        result.put("brand", brand);

        // productList
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        productExample.or().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsId);
        List<MarketGoodsProduct> productList = productMapper.selectByExample(productExample);
        // 添加result
        result.put("productList", productList);

        // info
        // 在查询brandId时查询过，直接用
        result.put("info", goods);

        // 添加用户足迹
        if (userId != null) {
            MarketFootprint footprint = new MarketFootprint();
            MarketFootprintExample footprintExample = new MarketFootprintExample();
            footprintExample.or().andDeletedEqualTo(false).andUserIdEqualTo(userId).andGoodsIdEqualTo(goodsId);
            List<MarketFootprint> footprintList = footprintMapper.selectByExample(footprintExample);
            footprint.setUpdateTime(LocalDateTime.now());
            footprint.setAddTime(LocalDateTime.now());
            if (footprintList.size() == 0){
                // 没有足迹记录
                footprint.setUserId(userId);
                footprint.setGoodsId(goodsId);
                footprint.setDeleted(false);
                footprintMapper.insertSelective(footprint);
            }else {
                // 有足迹记录，更新时间
                footprintMapper.updateByExampleSelective(footprint,footprintExample);
            }
        }


        return result;
    }

    @Override
    public List<NewHotGoodsListVo> getGoodsRelated(Integer goodsId) {

        MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
        Integer categoryId = goods.getCategoryId();

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.or().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId);

        List<MarketGoods> goodsList = goodsMapper.selectByExample(goodsExample);

        List<NewHotGoodsListVo> result = new ArrayList<>();
        for (MarketGoods marketGoods : goodsList) {
            NewHotGoodsListVo goodsListVo = new NewHotGoodsListVo();
            goodsListVo.setId(marketGoods.getId());
            goodsListVo.setName(marketGoods.getName());
            goodsListVo.setBrief(marketGoods.getBrief());
            goodsListVo.setPicUrl(marketGoods.getPicUrl());
            goodsListVo.setIsNew(marketGoods.getIsNew());
            goodsListVo.setIsHot(marketGoods.getIsHot());
            goodsListVo.setCounterPrice(marketGoods.getCounterPrice());
            goodsListVo.setRetailPrice(marketGoods.getRetailPrice());

            result.add(goodsListVo);
        }

        return result;
    }
}
